package com.ksd.modules.download.controller;

import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.download.po.AdminDownloadsCategorySavePO;
import com.ksd.modules.download.po.AdminDownloadsCategorySearchPO;
import com.ksd.modules.download.service.IAdminDownloadsCategoryService;
import com.ksd.modules.download.vo.AdminDownloadsCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminDownloadsCategoryController extends BaseController {

    @Autowired
    private IAdminDownloadsCategoryService adminDownloadsCategoryService;

    /**
     * 【下载】- 查询分类列表
     * @param adminDownloadsCategorySearchPO
     * @return
     */
    @PostMapping("/download/category/treeall")
    public R<List<AdminDownloadsCategoryVo>> findAdminDownloadsCategoryAll(@RequestBody AdminDownloadsCategorySearchPO adminDownloadsCategorySearchPO){
        return R.data(adminDownloadsCategoryService.findAdminDownloadsCategory(adminDownloadsCategorySearchPO));
    }

    /**
     * 【下载】- 查询分类列表--只查询发布的分类
     * @param adminDownloadsCategorySearchPO
     * @return
     */
    @PostMapping("/download/category/tree")
    public R<List<AdminDownloadsCategoryVo>> findAdminDownloadsCategory(@RequestBody AdminDownloadsCategorySearchPO adminDownloadsCategorySearchPO){
        adminDownloadsCategorySearchPO.setStatus(StatusEnums.STATUS.getCode());
        return R.data(adminDownloadsCategoryService.findAdminDownloadsCategory(adminDownloadsCategorySearchPO));
    }

    /**
     * 【下载】- 查询根分类
     * @return
     */
    @PostMapping("/download/category/root")
    public R<List<AdminDownloadsCategoryVo>> findAdminDownloadsCategoryRoot(){
        return R.data(adminDownloadsCategoryService.findAdminDownloadsCategoryRoot());
    }

    /**
     * 【下载】- 根据id删除分类
     * @return
     */
    @PostMapping("/download/category/del/{id}")
    @ApiLog("【下载】- 根据id删除分类")
    public R<Boolean> deleteAdminDownloadsCategoryById(@PathVariable("id")Long id){
        return R.data(adminDownloadsCategoryService.deleteAdminDownloadsCategoryById(id));
    }

    /**
     * 【下载】- 根据id获取分类信息
     * @return
     */
    @PostMapping("/download/category/get/{id}")
    public R<AdminDownloadsCategoryVo> getAdminDownloadsCategoryById(@PathVariable("id")Long id){
        return R.data(adminDownloadsCategoryService.getAdminDownloadsCategoryById(id));
    }

    /**
     * 【下载】- 保存和修改分类信息
     * @return
     */
    @PostMapping("/download/category/saveupdate")
    @ApiLog("【下载】- 保存和修改分类信息")
    public R<Boolean> saveUpdateAdminDownloadsCategory(@RequestBody AdminDownloadsCategorySavePO adminDownloadsCategorySavePO){
        return R.data(adminDownloadsCategoryService.saveUpdateAdminDownloadsCategory(adminDownloadsCategorySavePO));
    }

    /**
     * 【下载】- 修改状态
     *
     * @return
     */
    @PostMapping("/download/category/status/{id}/{field}/{value}")
    @ApiLog("【下载】- 分类状态修改")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminDownloadsCategoryService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminDownloadsCategoryService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【下载分类】- 复制数据
     * @param opid
     * @return
     */
    @PostMapping("/download/category/copy/{opid}")
    @ApiLog("【下载分类】- 复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminDownloadsCategoryService.copyData(opid));
    }

}
